﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using wrsm.wow;
using wrsm.wow.entity;
using wrsm.wow.exceptions;

namespace wrsm.client {

    /// <summary>
    /// World of Warcraft Installation Management Form,
    /// Just used to give wrsm context to where world of warcraft is installed.
    /// </summary>
    public partial class WowInstallManagementForm : Form {

        #region [Local Attributes]

        /// <summary>
        /// World of Warcraft Installation Utility
        /// </summary>
        private WowInstallationUtility _wowInstUtil;

        /// <summary>
        /// World of Warcraft Installation manager
        /// </summary>
        private WowInstallationManager _wowInstallManager;

        #endregion

        

        #region [Construction]

        /// <summary>
        /// Initialize WowInstallManagementForm
        /// </summary>
        public WowInstallManagementForm() {
            InitializeComponent();
            _wowInstUtil = new WowInstallationUtility();
        }

        /// <summary>
        /// Initialize WowInstallmanagementForm
        /// </summary>
        /// <param name="installManager">World of Warcraft Installation Manager</param>
        internal WowInstallManagementForm(WowInstallationManager installManager) : this() {
            //Set up wow install manager, and wireup events
            _wowInstallManager = installManager;
            WireupInstallManagerEvents();
        }

        #endregion

        #region [Form Physics]

        /// <summary>
        /// Adds a World of Warcraft Installation to the List View
        /// </summary>
        /// <param name="install">World of Warcraft Installation to add to the list</param>
        private void UpdateInstallationToList(WowInstallation install) {
            ListViewItem item = null;
            foreach (ListViewItem i in lstvInstallations.Items) {
                if (((WowInstallation)i.Tag).InstallLocation == install.InstallLocation) {
                    item = i;
                    //TODO: Update the information
                    i.Tag = install;
                    break;
                }
            }
            if (item == null) {
                item = AddInstallToList(install);
            }
            //Select/Highlight the installation
            item.Selected = true;
        }

        /// <summary>
        /// Add a world of warcraft installation to the list view
        /// </summary>
        /// <param name="install">world of warcraft installation object</param>
        /// <returns>the listview item added</returns>
        private ListViewItem AddInstallToList(WowInstallation install) {
            ListViewItem item = new ListViewItem(new string[] {
                install.InstallLocation,
                install.Default.ToString()
            });
            item.Tag = install;
            return item;
        }

        /// <summary>
        /// Refreshes the World of Warcraft Installation List
        /// </summary>
        private void RefreshInstallList() {
            //Refreshes World of Warcraft Installation List
            lstvInstallations.Items.Clear();
            foreach (WowInstallation install in _wowInstallManager.GetAllManagedInstallations()) {
                UpdateInstallationToList(install);
            }
        }

        #endregion

        #region [Form Events]

        /// <summary>
        /// Fires when user selects File->Detect World of Warcrft Installations from file menu
        /// </summary>
        private void detectWorldOfWarcraftInstallationsToolStripMenuItem_Click(object sender, EventArgs e) {
            //Fireup World of Warcraft Installation Detection Interface
            using (WowInstallationDetectForm detectForm = new WowInstallationDetectForm(_wowInstUtil, _wowInstallManager)) {
                detectForm.ShowDialog(this);
            }
        }

        /// <summary>
        /// Fires when user selects File->Add Installation from file menu
        /// </summary>
        private void addInstallationToolStripMenuItem_Click(object sender, EventArgs e) {
            throw new NotImplementedException();
        }

        #endregion

        #region [Install Manager Events]

        /// <summary>
        /// Wires up install manager events
        /// </summary>
        private void WireupInstallManagerEvents() {
            _wowInstallManager.InstallationAddedOrModified += new WowInstallationHandler(OnInstallAddedOrModified);
            _wowInstallManager.InstallationRemoved += new WowInstallationHandler(OnInstallRemoved);
        }

        /// <summary>
        /// Fires when an installation is added or modified
        /// </summary>
        /// <param name="install">wow installation that has been added or modified</param>
        private void OnInstallAddedOrModified(WowInstallation install) {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Fires when an installation has been removed
        /// </summary>
        /// <param name="install">wow installation that has been removed</param>
        private void OnInstallRemoved(WowInstallation install) {
            throw new NotImplementedException();
        }

        #endregion

        #region [The Guts]



        #endregion

    }
}
